﻿using System.Collections.Generic;
using System.Linq;
using Wicresoft.SelfService.DAO;
using NRemedy;
using NRemedy.Linq;
using System.Configuration;
using System;

namespace Wicresoft.SelfService.DAL
{
    public class ITSMAccidentDAL
    {
        private static readonly string ARServer = ConfigurationManager.AppSettings["ARServer"];
        private static readonly string ARUid = ConfigurationManager.AppSettings["ARUid"];
        private static readonly string ARPwd = ConfigurationManager.AppSettings["ARPwd"];
        /// <summary>
        /// 查询ITSM事件单
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns></returns>
        public List<ServiceDetail> getAccident(string loginID, string requestID, string description, Nullable<int> status, Nullable<DateTime> startTime, Nullable<DateTime> endTime)
        {
            List<ServiceDetail> list = new List<ServiceDetail>();
            IList<FJ_CTMPeopleJoinIncidents> itsmlist = new List<FJ_CTMPeopleJoinIncidents>();
            using (ARLoginContext context = new ARLoginContext(ARServer, ARUid, ARPwd))
            {
                ARSet<FJ_CTMPeopleJoinIncidents> aa = new ARSet<FJ_CTMPeopleJoinIncidents>(context, new ARServerDefaultFactory());
                var q = from s in aa
                        where (s.SelfServiceRequestID == null) && (s.Remedy_Login_ID == loginID)
                        select s;
                var quey = q.ToList().AsQueryable();
                if(!string.IsNullOrEmpty(requestID))
                {
                    quey = quey.Where(p => p.IncidentNumber.Contains(requestID));
                }
                if (!string.IsNullOrEmpty(description))
                {
                    quey = quey.Where(p => p.DetailedDecription.Contains(description));
                }
                if (status != null)
                {
                    quey = quey.Where(p => (int)p.Status == status);
                }
                if (startTime != null)
                {
                    quey = quey.Where(p => p.Submit_Date >= startTime);
                }
                if (endTime != null)
                {
                    quey = quey.Where(p => p.Submit_Date <= endTime);
                }
                itsmlist = quey.ToList();
                
                foreach (var item in itsmlist)
                {
                    ServiceDetail detail = new ServiceDetail();
                    detail.accountID = item.Remedy_Login_ID;
                    UserInfo user = new UserInfo();
                    user.company = item.ContactCompany;
                    user.accountID = item.Remedy_Login_ID;
                    user.dept = item.Department;
                    user.phone = item.PhoneNumber;
                    user.position = item.JobTitle;
                    detail.serviceRequestID = "";
                    detail.IncidentNumber = item.IncidentNumber;
                    detail.account = user;
                    detail.companyIDs = item.ContactCompany;
                    string des = item.DetailedDecription == null ? " " : item.DetailedDecription;
                    detail.description = des;
                    detail.BDDescription = des;
                    ServiceList serviceList = new ServiceList();
                    serviceList.applvl1 = item.Event1;
                    serviceList.applvl2 = item.Event2;
                    serviceList.applvl3 = item.Event3;
                    serviceList.serviceName = "";
                    detail.approveOwner = item.Assignee;
                    detail.stauts = (int)item.Status;
                    detail.createTime = item.Submit_Date;
                    detail.CurrentProcessor = item.Assignee;
                    detail.SubmitType = 2;//ITSM提交
                    list.Add(detail);
                }
            }
            return list;
        }
        /// <summary>
        /// 创建ITSM事件单
        /// </summary>
        /// <param name="serde"></param>
        public string createAccident(ServiceDetail serde)
        {
#if _ENABLE_INTERGRATION
            string description = string.Empty;
            INTStaging entry = new INTStaging();
            entry.s_Customer = serde.accountID;
            entry.s_Company = serde.companyIDs;
            entry.s_ServiceId = serde.serviceRequestID;
            entry.s_System = serde.appName;
            //服务请求
            if (serde.serviceType.partialName != "BreakDown")
            {
                entry.s_Property = Event_Properties.P_ServiceRequest;
                entry.s_Summary = serde.serviceType.serviceName;
                description = serde.description;
                entry.s_TypeL1 = serde.serviceType.applvl1;
                if (string.IsNullOrEmpty(serde.serviceType.applvl2))
                {
                    entry.s_TypeL2 = serde.appName;
                }
                else
                {
                    entry.s_TypeL2 = serde.serviceType.applvl2;
                }
                entry.s_TypeL3 = serde.serviceType.applvl3;
            }
            //用户故障
            else
            {
                entry.s_Property = Event_Properties.P_BreakDown;
                entry.s_Summary = serde.BDSummary;
                description = serde.BDDescription;
                entry.s_TypeL1 = serde.breakDownlvl1;
                entry.s_TypeL2 = serde.breakDownlvl2;
                entry.s_TypeL3 = serde.breakDownlvl3;
            }
            if (!string.IsNullOrEmpty(serde.os))
            {
                description = string.Concat(description, "  OS:", serde.os);
            }
            if (!string.IsNullOrEmpty(serde.browser))
            {
                description = string.Concat(description, "  Browser:", serde.browser);
            }
            if (!string.IsNullOrEmpty(serde.computerName))
            {
                description = string.Concat(description, "  ComputerName:", serde.computerName);
            }
            if (!string.IsNullOrEmpty(serde.ipAdress))
            {
                description = string.Concat(description, "  IP:", serde.ipAdress);
            }
            if (!string.IsNullOrEmpty(serde.mac))
            {
                description = string.Concat(description, "  MAC:", serde.mac);
            }
            entry.s_AdministrativeRegion = serde.AdministrativeRegion;
            entry.s_Description = description;
            entry.s_Driven = "SelfSeviceDrive";
            entry.Submitter = ARUid;
            entry.Short_Description = "自服务门户生成的事件单";
            using (ARLoginContext context = new ARLoginContext(ARServer, ARUid, ARPwd))
            {
                ARProxy<INTStaging> proxy = new ARProxy<INTStaging>(context, new ARServerCreate());
                string requestId = proxy.CreateEntry(entry);
                INTStaging staging = proxy.GetEntry(requestId);
                return staging.IncidentId;
                
            }
        }
#endif
        
    }
}
